﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DatabaseLayer;

namespace ControlLayer
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "OrderCtr" in both code and config file together.
    public class OrderCtr : IOrderCtr
    {
        static TablesDataContext db = new TablesDataContext();

        public void CreateOrder(string custId, double price, DateTime orderDate, DateTime departureDate, int[] routeIdList)
        {
            Order o = new Order();
            o.ssn = custId;
            o.price = price;
            o.orderDate = orderDate;
            o.departureDate = departureDate;

            int i = routeIdList.Length;

            //if(i==3)
            //{
            //    o.routeId = routeIdList[0].routeId;
            //    o.routeId1 = routeIdList[1].routeId;
            //    o.routeId2 = routeIdList[2].routeId;
            //}
            //else if (i == 2)
            //{
            //    o.orderId = routeIdList[0].routeId;
            //    o.routeId1 = routeIdList[1].routeId;
            //}
            //else
            //{
            //    o.routeId = routeIdList[0].routeId;
            //}

            if (i == 3)
            {
                o.routeId = routeIdList[0];
                o.routeId1 = routeIdList[1];
                o.routeId2 = routeIdList[2];
            }
            else if (i == 2)
            {
                o.orderId = routeIdList[0];
                o.routeId1 = routeIdList[1];
            }
            else
            {
                o.routeId = routeIdList[0];
            }

            db.Orders.InsertOnSubmit(o);

            db.SubmitChanges();
        }

        public _Order GetOrder(int orderId)
        {
            IQueryable<Order> orderQue =
                    from order in db.Orders
                    where order.orderId.Equals(orderId)
                    select order;

            _Order o = new _Order();
            o.custId = orderQue.ToList()[0].ssn;
            o.departureDate = (DateTime)orderQue.ToList()[0].departureDate;
            o.orderDate = (DateTime)orderQue.ToList()[0].orderDate;
            o.price = (double)orderQue.ToList()[0].price;

            RouteCtr rCtr = new RouteCtr();



            if(orderQue.ToList()[0].routeId2!=null)
            {
                o.routeList.Add(rCtr.GetRoute(orderQue.ToList()[0].routeId));
                o.routeList.Add(rCtr.GetRoute((int)orderQue.ToList()[0].routeId1));
                o.routeList.Add(rCtr.GetRoute((int)orderQue.ToList()[0].routeId2));
            }
            else if(orderQue.ToList()[0].routeId1!=null)
            {
                o.routeList.Add(rCtr.GetRoute((int)orderQue.ToList()[0].routeId));
                o.routeList.Add(rCtr.GetRoute((int)orderQue.ToList()[0].routeId1));
            }
            else
            {
                o.routeList.Add(rCtr.GetRoute((int)orderQue.ToList()[0].routeId));
            }

            return o;

        }

        public string PassSomeText(string s)
        {
            return "Text + " + s;
        }
    }

    public class _Order
    {
        public string custId { set; get; }
        public double price { set; get; }
        public DateTime orderDate { set; get; }
        public DateTime departureDate { set; get; }
        public List<DatabaseLayer.Flightroute> routeList { set; get; }

        public _Order(string custId, double price, DateTime orderDate, DateTime departureDate, List<DatabaseLayer.Flightroute> routeList)
        {
            this.custId = custId;
            this.price = price;
            this.orderDate = orderDate;
            this.departureDate = departureDate;
            this.routeList = routeList;
        }

        public _Order() { }

    }
}
